﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpiderRelation0720.Help;
using System.Data.Linq;
using SpiderRelation0720.Help;

namespace SpiderRelation0720.DbUpdate
{
    class DbUpdateControl
    {
        public int  AddCourseRecord(List<RCourseRecord> record) //插入课程记录到数据库
        {
            //插入数据到数据库
            //返回正确插入到数据库的条数
            int count = 0;
            foreach (RCourseRecord temp in record)
            {
                //首先获得建筑物编号
                int buildingId = GetBuildingId(temp.area, temp.buildingMark);
                if (buildingId < 0)
                {
                    //如果数据库不存在教学楼编号，则取消此次数据插入
                    continue;
                }
                else { 
                    //插入数据到数据库表：TeachClassRoom(可能会有), TeachCourseRecord
                    try
                    {
                        int classRoomId = GetClassRoomId(buildingId, temp.classRoom, temp.floorNumber);
                        if (classRoomId == -1)
                        {
                            //系统错误
                            return -1;   
                        }
                        else 
                        {
                            //更新数据TeachCourseRecord
                            DigitwhuDataContext dbContext = new DigitwhuDataContext();  
                            TeachCourceRecord recordCourse = new TeachCourceRecord();
                            recordCourse.classRoomId = classRoomId; //上课的教室编号
                            recordCourse.dayOfTheWeek =byte.Parse( temp.dayOfTheWeek.ToString());    //星期几
                            recordCourse.startOfWeek =byte.Parse( temp.startOfWeek.ToString());  //开始周数
                            recordCourse.endOfWeek = byte.Parse(temp.endOfWeek.ToString()); //结束周数
                            recordCourse.startNumberOfClass = byte.Parse(temp.startNumberOfClass.ToString());  //上课的开始节数
                            recordCourse.endNumberOfCalss = byte.Parse(temp.endNumberOfClass.ToString());   //上课的结束节数
                            dbContext.TeachCourceRecord.InsertOnSubmit(recordCourse);
                            dbContext.SubmitChanges();      //更新到数据库
                            dbContext.Dispose();            //释放资源
                            count++;    //插入条数增加
                            
                        }
                    }
                    catch (Exception e)
                    {
                        continue;   //数据库异常，可能已经存在相应的记录，此次插入失败
                    }
                }
            }
            return count;  //返回最后一条记录的更新结果
        }
        private int GetBuildingId(int area, string buildingMark)//根据校区和教学楼编号获得地图上面的建筑物编号
        {
            string key = area + "-" + buildingMark;
            int buildingId = BuildingDictionary.GetTeacherBuildingId(key);
            return buildingId;  //-1表示数据库中不存在相应的教学楼编号
        }
        private int GetClassRoomId(int buildingId,int classRoom,int floorNumber)
        {
            //如果此间教室已经存在，返回教室编号
            //如果此间教室不存在，则创建此教室，并返回最新的教室编号
            DigitwhuDataContext dbContext=new DigitwhuDataContext();
            int roomId = GetFindClassRoomId(buildingId, classRoom, floorNumber);
            if (roomId > 0)
            {
                //存在教室编号则返回
                return roomId;
            }
            //不存在则创建记录
            TeachClassRoom temp = new TeachClassRoom();
            temp.buildingId = buildingId;
            temp.classRoom = classRoom;
            temp.floorNumber =byte.Parse( floorNumber.ToString());
            dbContext.TeachClassRoom.InsertOnSubmit(temp);
            dbContext.SubmitChanges();  //发生异常未处理
            roomId = GetFindClassRoomId(buildingId, classRoom, floorNumber);
            if (roomId > 0)
            {
                return roomId;  //再次验证
            }
            else { 
                //系统发生错误，未处理
                return -1;
            }
        }
        private int GetFindClassRoomId(int buildingId, int classRoom, int floorNumber)
        { 
            //查找某个教室的教室编号
            //返回值：正数表示教室编号，负数表示不存在该教室
            DigitwhuDataContext dbContext = new DigitwhuDataContext();
            var room = from c in dbContext.TeachClassRoom
                       where c.buildingId == buildingId && c.classRoom == classRoom && c.floorNumber == floorNumber
                       select c.id;
            foreach (int r in room)
            {
                //存在记录则放回该教室编号
                return r;
            }
            return - 1;
        }
        
    }
}
